package com.jyw.custom.huaxi.oa.service.impl;

import com.google.common.collect.Lists;
import com.jyw.common.constant.ConfigKeyConstant;
import com.jyw.common.constant.Constants;
import com.jyw.common.core.domain.entity.SysDept;
import com.jyw.common.core.domain.entity.SysUser;
import com.jyw.common.core.domain.model.LoginUser;
import com.jyw.common.exception.ServiceException;
import com.jyw.custom.huaxi.oa.domain.dto.HuaxiDeptPushDto;
import com.jyw.custom.huaxi.oa.domain.dto.HuaxiUserPushDto;
import com.jyw.custom.huaxi.oa.service.IHuaxiOaService;
import com.jyw.system.domain.dto.BaseUser;
import com.jyw.system.domain.dto.UserGrantDto;
import com.jyw.system.service.ISysConfigService;
import com.jyw.system.service.ISysDeptService;
import com.jyw.system.service.ISysRoleService;
import com.jyw.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * oa系统对接服务接口实现
 */
@Service
public class HuaxiOaService implements IHuaxiOaService {

    @Resource
    private ISysDeptService deptService;

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysConfigService configService;

    @Resource
    private ISysRoleService roleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void synchronizeDept(HuaxiDeptPushDto unmarshal) {
        List<HuaxiDeptPushDto.CTDept> deptList = unmarshal.getBody().getCtDeptList();
        //按照科室层级排序
        deptList.sort(((o1, o2) -> {
            int o1CodeLength = 0,o2CodeLength = 0;
            if(!ObjectUtils.isEmpty(o1.getCtdCode())){
                o1CodeLength = o1.getCtdCode().length();
            }
            if(!ObjectUtils.isEmpty(o2.getCtdCode())){
                o2CodeLength = o2.getCtdCode().length();
            }
            return o1CodeLength-o2CodeLength;
        }));
        deptList.forEach(data->{
            SysDept sysDept = data.buildDept();
            if(ObjectUtils.isEmpty(sysDept)){
                throw new ServiceException("构建科室失败");
            }
            if(!ObjectUtils.isEmpty(sysDept.getDeptId())){
                //修改
                deptService.updateDept(sysDept);
            }else {
                //新增
                deptService.insertDept(sysDept);
            }
        });
    }

    @Override
    public void synchronizeUser(HuaxiUserPushDto unmarshal) {
        String roleId = configService.selectConfigByKey(ConfigKeyConstant.SYS_ALLOCATE_ROLE);
        List<HuaxiUserPushDto.CTCareProv> userList = unmarshal.getBody().getCtCareProvList();
        userList.forEach(data->{
            SysUser user = data.buildUser();
            SysDept dept = deptService.lambdaQuery().eq(SysDept::getDeptCode, data.getCtcpDeptHierarchyCode()).one();
            ArrayList<Long> deptIdList = Lists.newArrayList();
            if(!ObjectUtils.isEmpty(dept)){
                deptIdList = Lists.newArrayList(dept.getDeptId());
                user.setDept(dept);
            }
            if(!ObjectUtils.isEmpty(user.getUserId())){
                //修改
                userService.updateUser(user,deptIdList);
            }else{
                //新增
                userService.insertUser(user,deptIdList);
                //初始化角色信息绑定
                if(!ObjectUtils.isEmpty(dept)){
                    roleService.initDefaultRole(user.getUserId(),dept.getDeptId());
                }
            }
        });
    }


}
